(function($) {
	
	$.fn.SurveysView = function() {
		
	    /**
	     * 
	     */
	    this.showSurveyList = function(oData, context) {
	    	renderSurveysTemplate(oData, context);
	    	buildAndShowTable();
	    	addFilterHandles();
	    };
	    
	    this.refreshSurveysTable = function(oData, context) {
	    	$('#surveysTableArea').remove();
	    	context.$element().append(ich.surveysTable(oData));
	    	buildAndShowTable();
	    };
	    
	    this.showSurveyError = function(context, sError) {
	    	context.$element().html(ich.surveyDetailsHeader({}));
			$('.appStatus').text(sError)
			.addClass("ui-state-error");
	    };
	    
	    /**
	     * Renders the ICanHaz template for the survey list
	     */
	    function renderSurveysTemplate(oData, context) {
	    	context.$element().html(ich.surveysTableFilter(oData));
	    	context.$element().append(ich.surveysTable(oData));
	    };

	    /**
	     * Configures and shows the survey table
	     */
	    function buildAndShowTable() {
	    	$('#surveysTable').dataTable({
	    		"aaSorting": [[ 0, "asc" ]],
	    		"bFilter": true,
	    		"bJQueryUI": false,
                "oLanguage" :
                {
                	"sProcessing":   "Bitte warten...",
                	"sLengthMenu":   "_MENU_ Einträge anzeigen",
                	"sZeroRecords":  "Keine Einträge vorhanden.",
                	"sInfo":         "_START_ bis _END_ von _TOTAL_ Einträgen",
                	"sInfoEmpty":    "0 bis 0 von 0 Einträgen",
                	"sInfoFiltered": "(gefiltert von _MAX_  Einträgen)",
                	"sInfoPostFix":  "",
                	"sSearch":       "Suchen",
                	"sUrl":          "",
                	"oPaginate": {
                		"sFirst":    "Erster",
                		"sPrevious": "Zurück",
                		"sNext":     "Nächster",
                		"sLast":     "Letzter"
                	}
                }
	    	});
	    };
	    
	    /**
	     * Adds click handles to the filter checkboxes
	     */
	    function addFilterHandles() {
	    	$('.statusCheckbox').each(function() {
	    		$(this).click(function() {
	    			$('#surveysTableFilterForm').submit();
	    		});
	    	});
	    };
	
	    /**
	     * 
	     */
	    this.showSurvey = function(context, oSurvey) {
	    	this.renderSurveyTemplate(oSurvey, context);
	    	initSurveyForm(oSurvey);
			addSurveyFormHandles(oSurvey);
	    };
	    
	    /**
	     * Renders the ICanHaz template for a survey
	     */
	    this.renderSurveyTemplate = function(oData, context) {
	    	if(oData.questionnaire) {
		    	// this surveys' questionnaire gets selected in the questionnaires drop down
		    	for(var i = 0; i < oData.questionnaires.length; i++) {
		    		if(oData.questionnaires[i].title == oData.questionnaire.title)
		    			oData.questionnaires[i].selected = true;
		    	}
	    	}
	    	context.$element().html(ich.surveyDetailsHeader(oData));
	    	context.$element().append(ich.surveyDetails(oData));
			$('input:submit').button();
	    };
	    
	    /**
	     * 
	     */
	    function initSurveyForm(oSurvey) {
	    	// form initialization based on survey data
			$( '#validFrom' ).datepicker( { dateFormat: 'dd.mm.yy' } );
			$( '#validTo' ).datepicker( { dateFormat: 'dd.mm.yy' } );		
	    	
			$( '#periodic' ).attr( 'checked', oSurvey.periodic );
			$( '#period' ).attr( 'disabled', ! oSurvey.periodic );
			$( '#periodUnit' ).attr( 'disabled', ! oSurvey.periodic );
			$( '#periodUnit option[value="' + oSurvey.periodUnit + '"]' ).attr( 'selected', oSurvey.periodic );
			
			if( oSurvey.isPrivate ) {
				$( '#isPrivate' ).attr( 'checked', 'checked' );
			} else {
				$( '#privateLink' ).attr( 'disabled', true ).val( '' );
				$( '#privateLink' ).val("");
			}
			
			$( '#withKeys' ).attr( 'checked',  oSurvey.withKeys );
			val = "";
			for( i = 0; i <  oSurvey.keys.length; i++ ) {					
				val = val +  oSurvey.keys[i];
				if( i <  oSurvey.keys.length - 1 )
					val = val + '\n';
			}
			$( '#keys' ).attr( 'disabled', !  oSurvey.withKeys ).val( val );
			
			// evaluate status
			if(oSurvey.status == "archived") {
				$( '.surveyFormInput' ).attr( 'disabled', true );
				$( '#archive' ).attr( 'checked', 'checked' );
			} else if(oSurvey.status == "active") {
				$( '.surveyFormInput' ).attr( 'disabled', true );
				$( '#stop' ).attr( 'disabled', false );
			} else if(oSurvey.status == "stopped") {
				$( '#stop' ).attr( 'checked', 'checked' );
			}
			
	    };
		
	    /**
	     * 
	     */
	    function addSurveyFormHandles(oSurvey) {
			$( '#periodic' ).click( function() {
				$( '#period' ).attr( 'disabled', $( this ).attr( 'checked' ) === false );
				$( '#periodUnit' ).attr( 'disabled', $( this ).attr( 'checked' ) === false );
			} );			
			
			$( '#isPrivate' ).click( function() {
				if( $( this ).attr( 'checked' ) === false )
					$( '#privateLink' ).attr( 'disabled', true ).val( '' );
				else
					$( '#privateLink' ).attr( 'disabled', false ).val( oSurvey.privateLink );
			} );			
			
			$( '#withKeys' ).click( function() {
				evalKeys(oSurvey);
			} );			
			
			$( '#archive' ).click( function() {
				if( $( this ).attr( 'checked' ) === false ) {
					$( '.surveyFormInput' ).attr( 'disabled', false );
					$( '#period' ).attr( 'disabled', $( '#periodic' ).attr( 'checked' ) === false );
					$( '#periodUnit' ).attr( 'disabled', $( '#periodic' ).attr( 'checked' ) === false );
					if( $( '#isPrivate' ).attr( 'checked' ) === false )
						$( '#privateLink' ).attr( 'disabled', true ).val( '' );
					else
						$( '#privateLink' ).attr( 'disabled', false ).val( oSurvey.privateLink );
					evalKeys(oSurvey);
				}
				else {
					$( '.surveyFormInput' ).attr( 'disabled', true );
				}
			} );
			
			$( '#stop' ).click( function() {
				if( $( this ).attr( 'checked' ) === true ) {
					$( '.surveyFormInput' ).attr( 'disabled', false );
					$( '#period' ).attr( 'disabled', $( '#periodic' ).attr( 'checked' ) === false );
					$( '#periodUnit' ).attr( 'disabled', $( '#periodic' ).attr( 'checked' ) === false );
					if( $( '#isPrivate' ).attr( 'checked' ) === false )
						$( '#privateLink' ).attr( 'disabled', true ).val( '' );
					else
						$( '#privateLink' ).attr( 'disabled', false ).val( oSurvey.privateLink );
					evalKeys(oSurvey);
				}
				else {
					if(oSurvey.status == "active") {
						$( '.surveyFormInput' ).attr( 'disabled', true );
						$( '#stop' ).attr( 'disabled', false );
					}
				}
			} );
	    	
	    };
	    
		function evalKeys(oSurvey) {
			if( $( '#withKeys' ).attr( 'checked' ) === false ) {
				$( '#keys' ).attr( 'disabled', true ).val( "" );
			} else {
				val = "";
				for( i = 0; i <  oSurvey.keys.length; i++ ) {					
					val = val +  oSurvey.keys[i];
					if( i <  oSurvey.keys.length - 1 )
						val = val + '\n';
				}
				$( '#keys' ).attr( 'disabled', false ).val( val );
			}
		};
	}
})(jQuery);
